package com.ornate.core.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ResultUtils {

	// Pluck
	public static List<Map<String, Object>> pluck(List<Map<String, Object>> result, String field, Object value) {
		List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
		Map<String, Object> record;

		for (Iterator<Map<String, Object>> iterator = result.iterator(); iterator.hasNext();) {
			record = iterator.next();
			if (record.get(field).equals(value)) records.add(record);
		}

		return records;
	}

	public static List<Map<String, Object>> pluck(List<Map<String, Object>> result, String field, List<Object> value) {
		List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
		Map<String, Object> record;

		for (Iterator<Map<String, Object>> iterator = result.iterator(); iterator.hasNext();) {
			record = iterator.next();
			if (value.contains(record.get(field))) records.add(record);
		}

		return records;
	}

	public static List<Map<String, Object>> pluck(List<Map<String, Object>> result, String field, Object[] value) {
		return pluck(result, field, Arrays.asList(value));
	}

	// Intersect
	public static List<Map<String, Object>> intersect(List<Map<String, Object>> resultA, List<Map<String, Object>> resultB, List<Map<String, Object>>... results) {
		return null;
	}

	public static List<Map<String, Object>> intersect(List<Map<String, Object>> resultA, List<Map<String, Object>> resultB, String intersectProperty) {
		return null;
	}

	// Difference
	public static List<Map<String, Object>> difference(List<Map<String, Object>> resultA, List<Map<String, Object>> resultB) {
		return null;
	}

	public static List<Map<String, Object>> difference(List<Map<String, Object>> resultA, List<Map<String, Object>> resultB, String differenceProperty) {
		return null;
	}

	// ToMap
	public static Map<String, Object> toMap(List<Map<String, Object>> result, String keyProperty) {
		Map<String, Object> map = new HashMap<String, Object>(result.size()), record = null;

		for (Iterator<Map<String, Object>> iterator = result.iterator(); iterator.hasNext();) {
			record = iterator.next();
			map.put((String) record.get(keyProperty), record);
		}

		return map;
	}

	public static Map<String, Object> toMap(List<Map<String, Object>> result, String keyProperty, String valueProperty) {
		Map<String, Object> map = new HashMap<String, Object>(result.size()), record = null;

		for (Iterator<Map<String, Object>> iterator = result.iterator(); iterator.hasNext();) {
			record = iterator.next();
			map.put((String) record.get(keyProperty), record.get(valueProperty));
		}

		return map;
	}

	// Group
	public static Map<String, List<Map<String, Object>>> group(List<Map<String, Object>> result, String groupProperty) {
		Map<String, List<Map<String, Object>>> groups = new HashMap<String, List<Map<String, Object>>>();
		Map<String, Object> rec;
		String key = null;

		for (Iterator<Map<String, Object>> iterator = result.iterator(); iterator.hasNext();) {
			rec = iterator.next();
			key = rec.get(groupProperty).toString();
			if (!groups.containsKey(key)) {
				groups.put(key, new ArrayList<Map<String, Object>>());
			}
			groups.get(key).add(rec);
		}

		return groups;
	}

	// Sort
	public static List<Map<String, Object>> sortASC(List<Map<String, Object>> result, String sortProperty) {
		Map<String, Object> x, y;
		int size = result.size();

		for (int i = size; i > 0; i--) {
			for (int j = size - 1; j > 0; j--) {
				x = result.get(j);
				y = result.get(j - 1);
				if (x.get(sortProperty).toString().compareTo(y.get(sortProperty).toString()) < 0) {
					result.set(j, y);
					result.set(j - 1, x);
				}
			}
		}
		return result;
	}

	public static List<Map<String, Object>> sortDESC(List<Map<String, Object>> result, String sortProperty) {
		Map<String, Object> x, y;
		int size = result.size();

		for (int i = size; i > 0; i--) {
			for (int j = size - 1; j > 0; j--) {
				x = result.get(j);
				y = result.get(j - 1);
				if (x.get(sortProperty).toString().compareTo(y.get(sortProperty).toString()) > 0) {
					result.set(j, y);
					result.set(j - 1, x);
				}
			}
		}
		return result;
	}

}
